package com.gy.paidao.main;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gy.paidao.R;
import com.gy.paidao.bean.BaseBean;
import com.jky.struct2.http.FinalHttp;
import com.jky.struct2.http.core.AjaxCallBack;
import com.jky.struct2.http.core.AjaxParams;
import com.jky.struct2.http.entityhandle.HttpExceptionResult;
import com.jky.struct2.http.entityhandle.HttpResult;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by yingjun on 15/5/19.
 */
public class BaseFragment extends Fragment implements DialogInterface.OnCancelListener {

    protected Context mContext;
    private Toast toast;
    protected ProgressDialog progressDialog = null;
    protected Dialog loadingDialog = null;
    protected FinalHttp httpRequest;
    protected Gson gson;
    protected View _loading;
    protected AjaxParams params;
    private TextView _loadMsg;

    protected AjaxCallBack<HttpResult> callBack = new AjaxCallBack<HttpResult>() {
        @Override
        public void onSuccess(HttpResult httpResult) {
            dismissLoading();
            System.out.println("---------解析成功：---------->>" + httpResult.baseJson);

            try {
                JSONObject object = new JSONObject(httpResult.baseJson);
                BaseBean baseBean = new BaseBean();
                baseBean.success = object.optBoolean("success");
                baseBean.data = object.opt("data");
                baseBean.errCode = object.optString("errCode");
                baseBean.errMsg = object.optString("errMsg");
                baseBean.intData = object.optInt("data");
                baseBean.strData = object.optString("data");
                if (baseBean == null) {
                    handleNetErr(httpResult.which, httpResult.code);
                } else {
                    handlerBean(httpResult.which, baseBean);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        ;

        @Override
        public void onFailure(int which, HttpExceptionResult result) {
            dismissLoading();
            System.out.println("---------解析失败：-------->>" + result.msg);
            handleNetErr(which, result.code);
        }

        ;
    };

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = getActivity();

        _loading = View.inflate(mContext,R.layout.view_loading_layout,null);
        _loadMsg = (TextView) _loading.findViewById(R.id.view_loading_tv_msg);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initVariable();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void initVariable() {
        gson = new Gson();
        httpRequest = new FinalHttp(mContext);
        httpRequest.configTimeout(3 * 60 * 1000);
    }

    /**
     * 做一些初始化操作
     */
    protected void init() {

    }

    /**
     * 初始化view
     */
    protected void initView() {
    }

    ;

    /**
     * 设置一些view的监听
     */
    protected void setListener() {

    }

    /**
     * 填充页面数据
     */
    protected void fillData() {

    }

    /**
     * 初始化加载数据
     */
    protected void loadData(int reqeustFlag) {
    }

    /**
     * toast string消息,时间2秒
     */
    protected void showToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
            toast.show();
        } else {
            toast.setText(msg);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();
        }
    }

    /**
     * toast string消息,时间2秒
     */
    protected void showToast(int resId) {
        if (toast == null) {
            toast = Toast.makeText(mContext, resId,
                    Toast.LENGTH_SHORT);
            toast.show();
        } else {
            toast.setText(resId);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();
        }
    }

    /**
     * 显示loading框
     */
    public void showLoadDialog() {
        showLoadDialog("");
    }

    public void showLoadDialog(int flag) {
        showLoadDialog("", flag);
    }

    public void showLoadDialog(String msg) {
        showLoadDialog(msg, 1);
    }

    public void showLoadDialog(String msg, int flag) {
        if (TextUtils.isEmpty(msg)) {
            msg = "加载中...";
        }
        if (loadingDialog == null) {
            if (flag == 0) {
                loadingDialog = new Dialog(mContext, R.style.dialog_loading);
            } else if (flag == 1) {
                loadingDialog = new Dialog(mContext, R.style.dialog_loading_default);
            }
            loadingDialog.setCanceledOnTouchOutside(false);
        }
        loadingDialog.setContentView(_loading, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        loadingDialog.show();
    }


    /**
     * 关闭loading框
     */
    public void dismissLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    protected String getUMengTagName() {
        return getClass().getName();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void onCancel(DialogInterface dialog) {
        //dialog取消监听
    }

    protected void handlerBean(int requestCode, BaseBean baseBean) {

    }

    /**
     * 网络错误处理
     *
     * @param requestCode 请求
     * @param errorCode   1:网络未连接 2：
     */
    public void handleNetErr(int requestCode, int errorCode) {

    }
}
